﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileManagerBase;
using FileManagerLibrary.Base;
using System.IO;
using Logging;
using FileManagerLibrary.Base.UIData;
using FileManagerLibrary.Base.FileBase;

namespace FileManagerTool
{
    public partial class BackupJobManagerView : Form
    {
        [Serializable]
        public delegate bool MyEventHandler<TEventArgs>(object sender, TEventArgs e);
        /// <summary>
        /// Fires when folder is updated.
        /// </summary>
        public event MyEventHandler<GenericEventArgsType<BackupJob>> DataSaved;

        internal BackupJob CurrentJob { get; set; }
        internal bool IsUILoading { get; set; } //Can be used to disable messages due to UI status changes during Load of data.
        public FileManagerController FileManager { get; set; }
        public FolderManagerController FolderManager { get; set; }
        bool IsEditMode { get; set; }
        public string OriginalRootBackUpFolder { get; set; }

        public BackupJobManagerView(BackupJob currentJobSet,bool editMode,BackupJobModeEnum? defaultTab)
        {
            InitializeComponent();
            IsUILoading = true;
            CurrentJob = currentJobSet;
            IsEditMode = editMode;
            if (editMode == false) //in addnew mode
            {
                FileManager = new FileManagerController(this, currentJobSet, editMode);
                FolderManager = new FolderManagerController(this, currentJobSet, editMode);
                CurrentBackupJobMode = BackupJobModeEnum.Folders;
                if (defaultTab == BackupJobModeEnum.Folders)
                {
                    tbMain.SelectedTab = tbFolders;
                }
                else
                {
                    tbMain.SelectedTab = tbSingleFile;
                }
            }
            else //defaultTab must be null in edit mode
            {
                if (currentJobSet.CurrentBackupJobMode == BackupJobModeEnum.Folders)
                {
                    FolderManager = new FolderManagerController(this, currentJobSet, editMode);
                    tbMain.SelectedTab = tbFolders;
                    tbMain.TabPages.Remove(tbSingleFile);
                    CurrentBackupJobMode = BackupJobModeEnum.Folders;
                }
                else if (currentJobSet.CurrentBackupJobMode == BackupJobModeEnum.Files)
                {
                    FileManager = new FileManagerController(this, currentJobSet, editMode);
                    tbMain.SelectedTab = tbSingleFile;
                    tbMain.TabPages.Remove(tbFolders);
                    CurrentBackupJobMode = BackupJobModeEnum.Files;
                }
            }
            IsUILoading = false;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (Saved == false)
            {
                if (MessageBox.Show("Close without making changes?", "Changes", MessageBoxButtons.YesNo) == DialogResult.No)
                    e.Cancel = !e.Cancel;
            }
            base.OnClosing(e);
        }

        bool Saved;
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tbMain.SelectedTab == tbFolders)
            {
                if (FolderManager.SaveData() == false)
                    return;
            }
            else
            {
                if (FileManager.SaveData() == false)
                    return;
            }
            if (DataSaved != null)
            {
                if (DataSaved(this, new GenericEventArgsType<BackupJob>(CurrentJob)) == true)
                {
                    Saved = true;
                    this.Close();
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderManager.HndlBrowseFolder();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            FolderManager.DeleteSelctedItem(lstExcludedFolders);
        }

        private void btnClipboard_Click(object sender, EventArgs e)
        {
            FolderManager.HndlCopyFromClipBoard();
        }
        
        private void CopyMode_CheckedChanged(object sender, EventArgs e)
        {
            FolderManager.HndlCopyModeCheckChanged(sender);
        }

        private void RdoRepeat_CheckedChanged(object sender, EventArgs e)
        {
            FolderManager.HandleOnFolderScheduleModeChanged();
        }

        private void chkDeleteEmptyFolders_CheckedChanged(object sender, EventArgs e)
        {
            FolderManager.HndlDeleteEmptyFoldersCheckChanged();
        }

 
        private void btnCopyCliptoDestination_Click(object sender, EventArgs e)
        {
            FolderManager.btnCopyCliptoDestination();
        }

        private void btnCopyfrmClipBoard_Click(object sender, EventArgs e)
        {
            FolderManager.HndlCopyfrmClipBoard();
        }

        private void btnAddFile_Click(object sender, EventArgs e)
        {
            FolderManager.HndlAddFile();
        }

        private void btnAddFolder_Click(object sender, EventArgs e)
        {
            FolderManager.HndlAddFolder();
        }

        /*
         * this will store the current job mode value so that
         * we do not store any values into the 'CurrentJob'
         */
        private BackupJobModeEnum _CurrentBackupJobMode;

        public BackupJobModeEnum CurrentBackupJobMode
        {
            get
            {
                return _CurrentBackupJobMode;
            }
            set
            {
                if (value == BackupJobModeEnum.Folders)
                {
                    tbMain.SelectedTab = tbFolders;
                    FolderManager.ReloadData();
                }
                else if (value == BackupJobModeEnum.Files)
                {
                    tbMain.SelectedTab = tbSingleFile;
                    FileManager.ReloadData();
                }
                _CurrentBackupJobMode = value;
            }
        }

        private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e)
        {
            if (MessageBox.Show("Do you want to change the mode of backup? All changes you made will be lost.", "Mode Change", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
                return;
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tbMain.SelectedTab == tbFolders)
            {
                CurrentBackupJobMode = BackupJobModeEnum.Folders;
            }
            else if (tbMain.SelectedTab == tbSingleFile)
            {
                CurrentBackupJobMode = BackupJobModeEnum.Files;
            }
        }

        private void OnFileModeScheduleMode_Changed(object sender, EventArgs e)
        {
            FileManager.HandleOnFileScheduleModeChanged();
        }

        private void chkFileSizeExceeded_CheckedChanged(object sender, EventArgs e)
        {
            FileManager.HandleOnFileSizeExceedChanged();
        }

        private void OnFileOperationMode_Changed(object sender, EventArgs e)
        {
            FileManager.HandleOnFileOperationModeChanged(sender);
        }

        private void btnDeleteFiles_Click(object sender, EventArgs e)
        {
            FileManager.DeleteSelctedItem(lstFileNames);
        }

        private void txtBrowse1_Click(object sender, EventArgs e)
        {
            FileManager.HndlAddFiles();
        }

        private void btnClipboard1_Click(object sender, EventArgs e)
        {
            FileManager.HndlCopyfrmClipBoard();
        }

        private void btnCopyCliptoDestination1_Click(object sender, EventArgs e)
        {
            FileManager.HndlbtnCopyCliptoDestination1Click();
        }

        private void btnbckLocation_Click(object sender, EventArgs e)
        {
            FolderManager.HndlbtnbckLocationClick();
        }
        
        private void btnbckLocation1_Click(object sender, EventArgs e)
        {
            FileManager.HndlbtnbckLocationClick();
        }

        private void txtBackupSetName_Leave(object sender, EventArgs e)
        {
            FolderManager.HndlBackupSetNameLeave();
        }

        private void BackupJobManagerView_Load(object sender, EventArgs e)
        {

        }

        private void MouseDoubleClick_OpenFolder(object sender, MouseEventArgs e)
        {

            try
            {
                System.Diagnostics.Process.Start("explorer.exe", ((TextBox)sender).Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(Logging.Logger.StaticLogger.GetExceptionEntry(ex));
            }
        }
    }
}
